package com.swt.gap.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.swt.gap.common.constant.AchievementShareConstant;
import com.swt.gap.common.exception.ExpiredException;
import com.swt.gap.common.exception.InvalidPasswordException;
import com.swt.gap.dao.AchievementDao;
import com.swt.gap.dao.AchievementShareDao;
import com.swt.gap.model.dto.AchievementShareDTO;
import com.swt.gap.model.entity.Achievement;
import com.swt.gap.model.entity.AchievementShare;
import com.swt.gap.model.vo.achievementcenter.AchievementShareInfoVO;
import com.swt.gap.service.AchievementShareService;
import com.swt.gap.util.UserLoginUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.Duration;
import java.time.LocalDateTime;

@Service
public class AchievementShareServiceImpl implements AchievementShareService {


    private final AchievementShareDao achievementShareDao;
    private final AchievementDao achievementDao;

    public AchievementShareServiceImpl(AchievementShareDao achievementShareDao, AchievementDao achievementDao) {
        this.achievementShareDao = achievementShareDao;
        this.achievementDao = achievementDao;
    }

    @Override
    public AchievementShare saveAchievementShare(AchievementShareDTO achievementShareDTO) {
        Integer loginUserId = UserLoginUtils.getUserId();
        Achievement achievement = achievementDao.selectByPrimaryKey(achievementShareDTO.getAchievementId());
        String password = achievementShareDTO.getHasPassword() ? SecureUtil.md5(achievementShareDTO.getPassword()) : null;
        AchievementShare achievementShare = AchievementShare.builder()
                .achievementId(achievementShareDTO.getAchievementId())
                .content(achievement.getContent())
                .createdBy(loginUserId)
                .createdTime(LocalDateTime.now())
                .validDate(achievementShareDTO.getValidDate())
                .password(password)
                .build();
        achievementShareDao.insertSelective(achievementShare);
//        achievementShare.setPassword(null);
        return achievementShare;
    }

    @Override
    public AchievementShare getAchievementShare(Integer achievementShareId, String password) throws InvalidPasswordException, ExpiredException {
        AchievementShare achievementShare = achievementShareDao.selectByPrimaryKey(achievementShareId);
        LocalDateTime createdTime = achievementShare.getCreatedTime();
        Integer validDate = achievementShare.getValidDate();
        LocalDateTime currentTime = LocalDateTime.now();
        if (!StringUtils.isEmpty(achievementShare.getPassword())
                && !achievementShare.getPassword().equals(SecureUtil.md5(password))) {
            throw new InvalidPasswordException();
        } else if (createdTime.plusDays(validDate).isBefore(currentTime)) {
            throw new ExpiredException();
        }
        achievementShare.setPassword(null);
        return achievementShare;
    }

    @Override
    public AchievementShareInfoVO getAchievementShareInfo(Integer achievementId) {
        AchievementShareInfoVO res = new AchievementShareInfoVO();
        AchievementShare achievementShare = achievementShareDao.selectLatestByAchievementId(achievementId);
        if (null != achievementShare) {
            LocalDateTime createdTime = achievementShare.getCreatedTime();
            Integer validDate = achievementShare.getValidDate();
            LocalDateTime currentTime = LocalDateTime.now();
            if (createdTime.plusDays(validDate).isAfter(currentTime)) {
                res.setAchievementShareId(achievementShare.getAchievementShareId());
                res.setPassword(achievementShare.getPassword());
                res.setRemainingValidDate((int)Duration.between(currentTime, createdTime.plusDays(validDate)).toDays());
            }
        }
        return res;
    }
}
